﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using GoDive.Contract;
using GoDive.Service;
using System.Linq.Expressions;
using GoDive.Common;

namespace GoDive.App
{

    public class ArticleController : ApiController
    {

        GoDiveDBContext db = new GoDiveDBContext();

        [HttpGet]
        public ResponseStatusContract GetList([FromUri]int pageindex, [FromUri]int pagesize)
        {
            try
            {
                if (pageindex <= 0)
                {
                    pageindex = 1;
                }

                if (pagesize <= 0)
                {
                    pagesize = 10;
                }

                var articleList = this.GetPagedList(pageindex, pagesize, 0);

                return ResponseStatusContract.GenSuccessResponse(articleList);
            }
            catch (Exception ex)
            {
                return ResponseStatusContract.GenServiceErrorResponse(ex);
            }
        }

        public ResponseStatusContract GetListByType([FromUri]int pageindex, [FromUri]int pagesize, [FromUri]int type)
        {
            try
            {
                if (pageindex <= 0)
                {
                    pageindex = 1;
                }

                if (pagesize <= 0)
                {
                    pagesize = 10;
                }

                PagedListContract<tarticle> articleList = this.GetPagedList(pageindex, pagesize, type);


                return ResponseStatusContract.GenSuccessResponse(articleList);
            }
            catch (Exception ex)
            {
                return ResponseStatusContract.GenServiceErrorResponse(ex);
            }
        }

        public PagedListContract<tarticle> GetPagedList(int pageindex, int pagesize, int type, Expression<Func<tarticle, bool>> where = null)
        {
            var query = db.tarticle.AsQueryable();

            if (where != null)
            {
                query = query.Where(where);
            }

            var pagedQuery = query.Join(db.tuser, outer => outer.UserID, inner => inner.ID, (outer, inner) => new
            {
                ID = outer.ID,
                Title = outer.Title,
                UserID = outer.UserID,
                UserName = inner.UserName,
                UserAvatar = inner.AvatarMiddle,
                ViewCount = outer.ViewCount,
                FavoriteCount = outer.FavoriteCount,
                CommentCount = outer.CommentCount,
                Summary = outer.Summary,
                Pic = outer.Pic,
                IsHome = outer.IsHome,
                DataChangeTime = outer.DataChangeTime
            });

            IOrderedQueryable<dynamic> orderedQuery;

            switch (type)
            {
                case 0:
                default:
                    orderedQuery = pagedQuery.OrderByDescending(x => x.FavoriteCount);
                    break;
                case 1:
                    orderedQuery = pagedQuery.OrderByDescending(x => x.DataChangeTime);
                    break;
                case 2:
                    orderedQuery = pagedQuery.OrderByDescending(x => x.IsHome);
                    break;
            }

            PagedListContract<dynamic> pagedList = new PagedListContract<dynamic>
            {
                PageIndex = pageindex,
                PageSize = pagesize
            };

            pagedList.Rows = orderedQuery.GetPagedList(pagedList).AsEnumerable();

            PagedListContract<tarticle> articleList = new PagedListContract<tarticle>
            {
                PageIndex = pageindex,
                PageSize = pagesize,
                PageTotalCount = pagedList.PageTotalCount,
                Total = pagedList.Total,
                Rows = pagedList.Rows.Select(x => new tarticle
                {
                    ID = x.ID,
                    Title = x.Title,
                    UserID = x.UserID,
                    User = new tuser
                    {
                        ID = x.UserID,
                        AvatarMiddle = x.UserAvatar,
                        UserName = x.UserName
                    },
                    ViewCount = x.ViewCount,
                    FavoriteCount = x.FavoriteCount,
                    CommentCount = x.CommentCount,
                    Summary = x.Summary,
                    Pic = x.Pic,
                    DataChangeTime = x.DataChangeTime
                })
            };
            return articleList;
        }

    }
}
